package com.example.teststatisticesinterface.activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.example.teststatisticesinterface.R;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

class AppInfo {
	private String appLabel; // 应用程序标签
	private Drawable appIcon; // 应用程序图像
	private Intent intent; // 启动应用程序的Intent
							// ，一般是Action为Main和Category为Lancher的Activity
	private String pkgName; // 应用程序所对应的包名

	
	
	public AppInfo(String appLabel, Drawable appIcon, Intent intent, String pkgName) {
		super();
		this.appLabel = appLabel;
		this.appIcon = appIcon;
		this.intent = intent;
		this.pkgName = pkgName;
	}

	public AppInfo(String appLabel, Drawable appIcon, String pkgName) {
		super();
		this.appLabel = appLabel;
		this.appIcon = appIcon;
		this.pkgName = pkgName;
	}

	public String getAppLabel() {
		return appLabel;
	}

	public void setAppLabel(String appLabel) {
		this.appLabel = appLabel;
	}

	public Drawable getAppIcon() {
		return appIcon;
	}

	public void setAppIcon(Drawable appIcon) {
		this.appIcon = appIcon;
	}

	public Intent getIntent() {
		return intent;
	}

	public void setIntent(Intent intent) {
		this.intent = intent;
	}

	public String getPkgName() {
		return pkgName;
	}

	public void setPkgName(String pkgName) {
		this.pkgName = pkgName;
	}

}

public class DebuggableFalse extends Activity implements OnClickListener {

	private Button btn_AppAll, btn_AppDebuggableFalse;
	private ListView mlistView;
	
	private List<AppInfo> mList; // 所有已安装的包信息
	private List<AppInfo> mDebuggableList;// 所有debuggbable 为false的 应用列表

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browse_app_list);
		initView();
	}

	private void initView() {
		mlistView = (ListView) findViewById(R.id.listviewApp);

		btn_AppAll = (Button) findViewById(R.id.btn_all);
		btn_AppAll.setOnClickListener(this);

		btn_AppDebuggableFalse = (Button) findViewById(R.id.btn_debuggable);
		btn_AppDebuggableFalse.setOnClickListener(this);

		mList = new ArrayList<AppInfo>();
		mDebuggableList = new ArrayList<AppInfo>();

		// queryAppInfo();
		// BrowseApplicationAdapter browseApplicationAdapter = new
		// BrowseApplicationAdapter(DebuggableFalse.this, mList);
		// mlistView.setAdapter(browseApplicationAdapter);
		// mlistView.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		// long arg3) {
		// Intent intent = mList.get(arg2).getIntent();
		// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
		// intent.FLAG_ACTIVITY_SINGLE_TOP);
		// startActivity(intent);
		// }
		// });

	}

	/**
	 * 完成 List<AppInfo> mList 内容的填充
	 */
	public void queryAppInfo() {

		PackageManager pm = this.getPackageManager();
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(intent.CATEGORY_LAUNCHER);

		List<ResolveInfo> resolves = pm.queryIntentActivities(intent, 0);
		// 调用系统排序 ， 根据name排序
		// 该排序很重要，否则只能显示系统应用，而不能列出第三方应用程序
		Collections.sort(resolves, new ResolveInfo.DisplayNameComparator(pm));
		if (mList != null) {
			mList.clear();
			for (ResolveInfo resolveInfo : resolves) {

				String activityName = resolveInfo.activityInfo.name;
				String packageName = resolveInfo.activityInfo.packageName;
				String lable = (String) resolveInfo.loadLabel(pm);
				Drawable icon = resolveInfo.loadIcon(pm);

				// 为应用程序的启动Activity 准备Intent
				Intent launchIntent = new Intent();
				launchIntent.setComponent(new ComponentName(packageName, activityName));

				AppInfo appInfo = new AppInfo(lable, icon, launchIntent, packageName);
				mList.add(appInfo);
			}

		}

	}

	/**
	 * 完成mDebuggableList的内容的填充
	 */

	public void queryAllDebuggableFalse() {

		PackageManager pm = this.getPackageManager();
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);

		List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
		Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));

		if (mDebuggableList != null) {
			mDebuggableList.clear();
			for (ResolveInfo resolveInfo : resolveInfos) {
				String packageName = resolveInfo.activityInfo.packageName.trim();
				String lable = (String) resolveInfo.loadLabel(pm);
				Drawable icon = resolveInfo.loadIcon(pm);
				String activityName = resolveInfo.activityInfo.name;

				AppInfo appInfo = new AppInfo(lable, icon, packageName);

				if (isApkDebugable(DebuggableFalse.this, packageName)) {
					mDebuggableList.add(appInfo);
				}
			}
		}
	}

	class BrowseApplicationAdapter extends BaseAdapter {
		private List<AppInfo> mlistAppInfos = null;
		private LayoutInflater inflater = null;

		public BrowseApplicationAdapter(Context cx, List<AppInfo> mlistAppInfos) {
			inflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.mlistAppInfos = mlistAppInfos;
		}

		@Override
		public int getCount() {
			return mlistAppInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return mlistAppInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = null;
			ViewHolder viewHolder = null;
			if (convertView == null || convertView.getTag() == null) {

				view = inflater.inflate(R.layout.browse_app_item, null);
				viewHolder = new ViewHolder(view);
				view.setTag(viewHolder);
			} else {
				view = convertView;
				viewHolder = (ViewHolder) convertView.getTag();
			}

			AppInfo appInfo = (AppInfo) this.getItem(position);
			viewHolder.appIcon.setImageDrawable(appInfo.getAppIcon());
			viewHolder.tvAppLabel.setText(appInfo.getAppLabel());
			viewHolder.tvPkgName.setText(appInfo.getPkgName());
			return view;
		}
	}

	class ViewHolder {
		ImageView appIcon;
		TextView tvAppLabel;
		TextView tvPkgName;

		public ViewHolder(View view) {
			this.appIcon = (ImageView) view.findViewById(R.id.imgApp);
			this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
			this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
		}
	}

	public static boolean isApkDebugable(Context context, String packageName) {
		try {
			PackageInfo pkginfo = context.getPackageManager().getPackageInfo(packageName, 1);
			if (pkginfo != null) {
				ApplicationInfo info = pkginfo.applicationInfo;
				return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
			}
		} catch (Exception e) {

		}
		return false;
	}
	
	private Intent testFloatViewIntent=null;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_all:
			
			testFloatViewIntent=new Intent();
			testFloatViewIntent.setClass(DebuggableFalse.this, MyFloatViewActivity.class);
			testFloatViewIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(testFloatViewIntent);
			
			queryAppInfo();
			BrowseApplicationAdapter browseApplicationAdapter = new BrowseApplicationAdapter(DebuggableFalse.this, mList);
			browseApplicationAdapter.notifyDataSetChanged();
			mlistView.setAdapter(browseApplicationAdapter);
			mlistView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
					Intent intent = mList.get(arg2).getIntent();
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | intent.FLAG_ACTIVITY_SINGLE_TOP);
					startActivity(intent);
				}
			});
			
			break;
		case R.id.btn_debuggable:
			queryAllDebuggableFalse();
			BrowseApplicationAdapter browseAppdebuggableFalse = new BrowseApplicationAdapter(DebuggableFalse.this, mDebuggableList);
			browseAppdebuggableFalse.notifyDataSetChanged();
			mlistView.setAdapter(browseAppdebuggableFalse);
			// mlistView.setOnItemClickListener(new OnItemClickListener() {
			//
			// @Override
			// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
			// long arg3) {
			// Intent intent = mList.get(arg2).getIntent();
			// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
			// intent.FLAG_ACTIVITY_SINGLE_TOP);
			// startActivity(intent);
			// }
			// });
			break;

		default:
			break;
		}

	}

}
